package ui;

import io.GPSPoint;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.Date;

import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;


/**
 * Draws a GPS point
 * 
 * @author Oliver Duerr
 */
public class GPSMarkerPoint implements MapMarker {

		private final GPSPoint gpsPoint;
		
		private final CurrentZoom currentZoom;

		public GPSMarkerPoint(GPSPoint gpsPoint, CurrentZoom currentZoom) {
			this.gpsPoint = gpsPoint;
			this.currentZoom = currentZoom;
		}
		
		@Override
		public double getLat() {
			return gpsPoint.getLatti();
		}

		@Override
		public double getLon() {
			return gpsPoint.getLongi();
		}

		@Override
		public void paint(Graphics arg0, Point arg1) {
			long[] hilite = currentZoom == null ? null : new long[] {currentZoom.getTimeStart(), currentZoom.getTimeEnd()}; 
			Graphics2D g2d = (Graphics2D) arg0;
			int x = arg1.x;
			int y = arg1.y;
			boolean doDarker = false;
			long time = gpsPoint.getTime();
			//System.out.println("in paint " + new Date(hilite[0]) + " " + new Date(hilite[1]));
			if (hilite != null && time > hilite[0] && time < hilite[1]) {
				doDarker = true;
			} 
			int rad = 2;
			Color color = Color.green;
			if (gpsPoint.getPrecission() <= 6) {
				color = Color.green;
			} else if (gpsPoint.getPrecission() <= 10) {
				color = Color.blue;
				rad = 2;
			} else if (gpsPoint.getPrecission() <= 30) {
				color = Color.yellow;
				rad = 3;
			} else {
				color = Color.red;
				rad = 4;
			}
			if (doDarker) {
				color = color.darker().darker();
			} else {
				color = color.brighter();
			}
			g2d.setColor(color);
			g2d.fillRect(x - rad, y - rad, 2 * rad, 2 *rad);
			
			//Painting the bearing
			double bearing = gpsPoint.getBearing();
			if (bearing > -380) {
				bearing = Math.PI * bearing / 180.0 ;
				int xTo = x + (int) ((rad + 5) * Math.cos(bearing)); 
				int yTo = y - (int) ((rad + 5) * Math.sin(bearing));
				g2d.drawLine(x, y, xTo, yTo);
			}
		}

	}